/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.invoice.controller;

import com.invoice.busines.ComprobanteBO;
import com.invoice.busines.ComprobanteBOImpl;
import com.invoice.busines.dto.ComprobanteDTO;
import com.invoice.exceptions.InvoiceException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.HorizontalBarChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartModel;
import org.primefaces.model.chart.DateAxis;
import org.primefaces.model.chart.LineChartSeries;
 

/**
 *
 * @author laura
 */
@ManagedBean
public class ChartView {
    private BarChartModel barModel;
    private HorizontalBarChartModel horizontalBarModel;
    private final ComprobanteBO comprobanteBO=new ComprobanteBOImpl();
  private LineChartModel dateModel;
    @PostConstruct
    public void init() {
        createDateModel();
    }
 
    public BarChartModel getBarModel() {
        return barModel;
    }     
 
    public LineChartModel getDateModel() {
        return dateModel;
    }
     
    private void createDateModel() {
        dateModel = new LineChartModel();
        LineChartSeries series1 = new LineChartSeries();
        series1.setLabel("Series 1");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            for (ComprobanteDTO comprobanteDTO : comprobanteBO.findGroupByFecha()) {
                String reportDate = df.format(comprobanteDTO.getFecha());
                if(comprobanteDTO.getTotal() != null){
                    series1.set(reportDate, Math.round(comprobanteDTO.getTotal().doubleValue()));
                }
            }
        } catch (InvoiceException ex) {
            Logger.getLogger(ChartView.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        dateModel.addSeries(series1);
         
        dateModel.setTitle("Ventas");
        dateModel.setZoom(true);
        dateModel.getAxis(AxisType.Y).setLabel("Soles");
        DateAxis axis = new DateAxis("Fechas");
        axis.setTickAngle(-50);
        //axis.setMax("2015-06-22");
        axis.setTickFormat("%b %#d, %y");
         
        dateModel.getAxes().put(AxisType.X, axis);
    }
}
